<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Returns an array with information about sunset/sunrise and twilight begin/end</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="function.date-sub.html">« date_sub</a></li>
      <li style="float: right;"><a href="function.date-sunrise.html">date_sunrise »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="ref.datetime.html">Date/Time 函数</a></li>
    <li>Returns an array with information about sunset/sunrise and twilight begin/end</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="function.date-sun-info" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">date_sun_info</h1>
  <p class="verinfo">(PHP 5 &gt;= 5.1.2, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">date_sun_info</span> &mdash; <span class="dc-title">Returns an array with information about sunset/sunrise and twilight begin/end</span></p>

 </div>
 <div class="refsect1 description" id="refsect1-function.date-sun-info-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>date_sun_info</strong></span>(<span class="methodparam"><span class="type">int</span> <code class="parameter">$timestamp</code></span>, <span class="methodparam"><span class="type">float</span> <code class="parameter">$latitude</code></span>, <span class="methodparam"><span class="type">float</span> <code class="parameter">$longitude</code></span>): <span class="type">array</span></div>

 </div>


 <div class="refsect1 parameters" id="refsect1-function.date-sun-info-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>
    
     <dt>
<code class="parameter">timestamp</code></dt>

     <dd>

      <p class="para">
       Unix timestamp.
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">latitude</code></dt>

     <dd>

      <p class="para">
       Latitude in degrees.
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">longitude</code></dt>

     <dd>

      <p class="para">
       Longitude in degrees.
      </p>
     </dd>

    
   </dl>

  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.date-sun-info-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   Returns array on success 或者在失败时返回 <strong><code>false</code></strong>.
   The structure of the array is detailed in the following list:
  </p>
  <p class="para">
   <dl>
    
     <dt>
<code class="literal">sunrise</code></dt>

     <dd>

      <span class="simpara">
       The timestamp of the sunrise (zenith angle = 90°35&#039;).
      </span>
     </dd>
 
    
    
     <dt>
<code class="literal">sunset</code></dt>

     <dd>

      <span class="simpara">
       The timestamp of the sunset (zenith angle = 90°35&#039;).
      </span>
     </dd>
 
    
    
     <dt>
<code class="literal">transit</code></dt>

     <dd>

      <span class="simpara">
       The timestamp when the sun is at its zenith, i.e. has reached its topmost
       point.
      </span>
     </dd>
 
    
    
     <dt>
<code class="literal">civil_twilight_begin</code></dt>

     <dd>

      <span class="simpara">
       The start of the civil dawn (zenith angle = 96°). It ends at
       <code class="literal">sunrise</code>.
      </span>
     </dd>
 
    
    
     <dt>
<code class="literal">civil_twilight_end</code></dt>

     <dd>

      <span class="simpara">
       The end of the civil dusk (zenith angle = 96°). It starts at
       <code class="literal">sunset</code>.
      </span>
     </dd>
 
    
    
     <dt>
<code class="literal">nautical_twilight_begin</code></dt>

     <dd>

      <span class="simpara">
       The start of the nautical dawn (zenith angle = 102°). It ends at
       <code class="literal">civil_twilight_begin</code>.
      </span>
     </dd>
 
    
    
     <dt>
<code class="literal">nautical_twilight_end</code></dt>

     <dd>

      <span class="simpara">
       The end of the nautical dusk (zenith angle = 102°). It starts at
       <code class="literal">civil_twilight_end</code>.
      </span>
     </dd>
 
    
    
     <dt>
<code class="literal">astronomical_twilight_begin</code></dt>

     <dd>

      <span class="simpara">
       The start of the astronomical dawn (zenith angle = 108°). It ends at
       <code class="literal">nautical_twilight_begin</code>.
      </span>
     </dd>
 
    
    
     <dt>
<code class="literal">astronomical_twilight_end</code></dt>

     <dd>

      <span class="simpara">
       The end of the astronomical dusk (zenith angle = 108°). It starts at
       <code class="literal">nautical_twilight_end</code>.
      </span>
     </dd>
 
    
   </dl>

  </p>
  <p class="para"> 
   The values of the array elements are either UNIX timestamps, <strong><code>false</code></strong> if the
   sun is below the respective zenith for the whole day, or <strong><code>true</code></strong> if the sun is
   above the respective zenith for the whole day.
  </p>
 </div>


 <div class="refsect1 changelog" id="refsect1-function.date-sun-info-changelog">
  <h3 class="title">更新日志</h3>
  <p class="para">
   <table class="doctable informaltable">
    
     <thead>
      <tr>
       <th>版本</th>
       <th>说明</th>
      </tr>

     </thead>

     <tbody class="tbody">
      <tr>
       <td>7.2.0</td>
       <td>
        The calculation was fixed with regards to local midnight instead of
        local noon, which changes the results slightly.
       </td>
      </tr>

     </tbody>
    
   </table>

  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.date-sun-info-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-2082">
    <p><strong>示例 #1 A <span class="function"><strong>date_sun_info()</strong></span> example</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$sun_info&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">date_sun_info</span><span style="color: #007700">(</span><span style="color: #0000BB">strtotime</span><span style="color: #007700">(</span><span style="color: #DD0000">"2006-12-12"</span><span style="color: #007700">),&nbsp;</span><span style="color: #0000BB">31.7667</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">35.2333</span><span style="color: #007700">);<br />foreach&nbsp;(</span><span style="color: #0000BB">$sun_info&nbsp;</span><span style="color: #007700">as&nbsp;</span><span style="color: #0000BB">$key&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">$val</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"</span><span style="color: #0000BB">$key</span><span style="color: #DD0000">:&nbsp;"&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">date</span><span style="color: #007700">(</span><span style="color: #DD0000">"H:i:s"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$val</span><span style="color: #007700">)&nbsp;.&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
sunrise: 05:52:11
sunset: 15:41:21
transit: 10:46:46
civil_twilight_begin: 05:24:08
civil_twilight_end: 16:09:24
nautical_twilight_begin: 04:52:25
nautical_twilight_end: 16:41:06
astronomical_twilight_begin: 04:21:32
astronomical_twilight_end: 17:12:00
</pre></div>
    </div>
   </div>
  </p>

  <p class="para">
   <div class="example" id="example-2083">
    <p><strong>示例 #2 Polar night, with some processing</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$tz&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;\</span><span style="color: #0000BB">DateTimeZone</span><span style="color: #007700">(</span><span style="color: #DD0000">'America/Anchorage'</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$si&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">date_sun_info</span><span style="color: #007700">(</span><span style="color: #0000BB">strtotime</span><span style="color: #007700">(</span><span style="color: #DD0000">"2022-12-21"</span><span style="color: #007700">),&nbsp;</span><span style="color: #0000BB">70.21</span><span style="color: #007700">,&nbsp;-</span><span style="color: #0000BB">148.51</span><span style="color: #007700">);<br />foreach&nbsp;(</span><span style="color: #0000BB">$si&nbsp;</span><span style="color: #007700">as&nbsp;</span><span style="color: #0000BB">$key&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">$value</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">match&nbsp;</span><span style="color: #007700">(</span><span style="color: #0000BB">$value</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">true&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'always'</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">false&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'never'</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;default&nbsp;=&gt;&nbsp;</span><span style="color: #0000BB">date_create</span><span style="color: #007700">(</span><span style="color: #DD0000">"@</span><span style="color: #007700">{</span><span style="color: #0000BB">$value</span><span style="color: #007700">}</span><span style="color: #DD0000">"</span><span style="color: #007700">)-&gt;</span><span style="color: #0000BB">setTimeZone</span><span style="color: #007700">(</span><span style="color: #0000BB">$tz</span><span style="color: #007700">)-&gt;</span><span style="color: #0000BB">format</span><span style="color: #007700">(&nbsp;</span><span style="color: #DD0000">'H:i:s&nbsp;T'&nbsp;</span><span style="color: #007700">),<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;},<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">":&nbsp;</span><span style="color: #007700">{</span><span style="color: #0000BB">$key</span><span style="color: #007700">}</span><span style="color: #DD0000">"</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
never: sunrise
never: sunset
12:52:18 AKST: transit
10:53:19 AKST: civil_twilight_begin
14:51:17 AKST: civil_twilight_end
09:01:47 AKST: nautical_twilight_begin
16:42:48 AKST: nautical_twilight_end
07:40:47 AKST: astronomical_twilight_begin
18:03:49 AKST: astronomical_twilight_end
</pre></div>
    </div>
   </div>
  </p>

  <p class="para">
   <div class="example" id="example-2084">
    <p><strong>示例 #3 Midnight sun (Tromsø, Norway)</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$si&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">date_sun_info</span><span style="color: #007700">(</span><span style="color: #0000BB">strtotime</span><span style="color: #007700">(</span><span style="color: #DD0000">"2022-06-26"</span><span style="color: #007700">),&nbsp;</span><span style="color: #0000BB">69.68</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">18.94</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">$si</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
Array
(
    [sunrise] =&gt; 1
    [sunset] =&gt; 1
    [transit] =&gt; 1656240426
    [civil_twilight_begin] =&gt; 1
    [civil_twilight_end] =&gt; 1
    [nautical_twilight_begin] =&gt; 1
    [nautical_twilight_end] =&gt; 1
    [astronomical_twilight_begin] =&gt; 1
    [astronomical_twilight_end] =&gt; 1
)
</pre></div>
    </div>
   </div>
  </p>

  <p class="para">
   <div class="example" id="example-2085">
    <p><strong>示例 #4 Calculating length of day (Kyiv)</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$si&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">date_sun_info</span><span style="color: #007700">(</span><span style="color: #0000BB">strtotime</span><span style="color: #007700">(</span><span style="color: #DD0000">'2022-08-26'</span><span style="color: #007700">),&nbsp;</span><span style="color: #0000BB">50.45</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">30.52</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$diff&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$si</span><span style="color: #007700">[</span><span style="color: #DD0000">'sunset'</span><span style="color: #007700">]&nbsp;-&nbsp;</span><span style="color: #0000BB">$si</span><span style="color: #007700">[</span><span style="color: #DD0000">'sunrise'</span><span style="color: #007700">];<br />echo&nbsp;</span><span style="color: #DD0000">"Length&nbsp;of&nbsp;day:&nbsp;"</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">floor</span><span style="color: #007700">(</span><span style="color: #0000BB">$diff&nbsp;</span><span style="color: #007700">/&nbsp;</span><span style="color: #0000BB">3600</span><span style="color: #007700">),&nbsp;</span><span style="color: #DD0000">"h&nbsp;"</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">floor</span><span style="color: #007700">((</span><span style="color: #0000BB">$diff&nbsp;</span><span style="color: #007700">%&nbsp;</span><span style="color: #0000BB">3600</span><span style="color: #007700">)&nbsp;/&nbsp;</span><span style="color: #0000BB">60</span><span style="color: #007700">),&nbsp;</span><span style="color: #DD0000">"s\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
Length of day: 13h 56s
</pre></div>
    </div>
   </div>
  </p>
 </div>


</div></div></div></body></html>